package com.viewhigh.yhrp;

import org.apache.commons.codec.binary.Base64;

/**   
* @Title: Decode.java 
* @Package com.viewhigh.yhrp 
* @Description: 解码
* @author zhangxiaofu   
* @Company www.viewhigh.com
* @date 2016年8月24日 下午2:53:01 
* @version V1.0   
*/
public class DeCodeUtil {
    /**
     * 
    * @Title: parsing 
    * @Description: 解析
    * @param @param str
    * @param @return    设定文件 
    * @return String    返回类型 
    * @throws
     */
    public static String parsing(String str) {
        int v,h;
        String hex = str.substring(0, v = str.indexOf(Const.A));
        hex = hexString2String(hex);
        String hex2 = str.substring(++v, h = str.indexOf(Const.B));
        hex2 = hexString2String(hex2);
        String hex3 = str.substring(++h, str.length());
        hex3 = hexString2String(hex3);
        hex = hex.replace(Const.base1, "");
        hex2 = deCross(hex2);
        hex3 = hex.replace(Const.base3, "");
        if(hex.equals(hex2) && hex.equals(hex3)){
            return hex;
        }else{
            return null;
        }
    }
    /**
     * 
    * @Title: parsing 
    * @Description: base64解 
    * @param @param str
    * @param @return    设定文件 
    * @return String    返回类型 
    * @throws
     */
    public static String parsingBase64(String str) {
        byte[] base = Base64.decodeBase64(str.getBytes());
        return parsing(new String(base));
    }
    
    /**
     * 
    * @Title: deCross 
    * @Description: 解交插
    * @param @param param
    * @param @return    设定文件 
    * @return String    返回类型 
    * @throws
     */
    public static String deCross(String param){
        char[] charArray = param.toCharArray();
        String temp = "";
        for ( int i  = 0 ; i< charArray.length ; i++){
            if(i%2 == 0){
                temp += charArray[i];
            }else{
                continue;
            }
        }
        return temp;
    }
    
    
    /** 
     * @Title:hexString2String 
     * @Description:16进制字符串转字符串 
     * @param src 16进制字符串
     * @return 字符串 
     * @throws 
     */  
    public static String hexString2String(String src) {  
        String temp = "";  
        for (int i = 0; i < src.length() / 2; i++) {  
            temp = temp  
                    + (char) Integer.valueOf(src.substring(i * 2, i * 2 + 2),  
                            16).byteValue();  
        }  
        return temp;  
    }  
}

